package com.yunduansing.core.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.yunduansing.core.commons.DbCommon;
import com.yunduansing.core.dto.BaseSearch;
import com.yunduansing.core.model.RoleMenuModel;
import com.yunduansing.core.model.RoleModel;
import com.yunduansing.core.service.MenuService;
import com.yunduansing.core.service.RoleService;
import com.yunduansing.core.utils.CommonUtil;

@Controller
@RequestMapping("/manager/role")
public class RoleController {
	@Autowired
	private RoleService service;
	@Autowired
	private MenuService roleService;
	
	@RequestMapping(value="index",method=RequestMethod.GET)
	public ModelAndView index(){
		ModelAndView view=new ModelAndView();
		view.setViewName("/beetl/role/index");
		return view;
	}
	
	@ResponseBody
	@RequestMapping(value="/page",method=RequestMethod.POST,produces="application/json;charset=utf-8")
	public String pagination(DbCommon.Grid grid,BaseSearch search){
		return CommonUtil.toJsonWithGson(service.pagination(grid, search));
	}
	
	@ResponseBody
	@RequestMapping(value="/save",method=RequestMethod.POST,produces="application/json;charset=utf-8")
	public String save(RoleModel model,@RequestParam(value="menuIds",required=false) String menuIds,
			@RequestParam(value="btnIds",required=false) String btnIds){
		List<RoleMenuModel> list=new ArrayList<RoleMenuModel>();
		String[] menuIdA=menuIds.split(",");
		String[] btnIdA=btnIds.split(",");
		if(menuIds.length()>0){
			for(int i=0;i<menuIdA.length;i++){
				RoleMenuModel item=new RoleMenuModel();
				if(model.getId()>0)
					item.setRoleId(model.getId());
				item.setMenuId(Integer.parseInt(menuIdA[i]));
				list.add(item);
			}
		}
		if(btnIds.length()>0){
			Map<String,ArrayList<String>> map=new HashMap<String,ArrayList<String>>();
			for(int i=0;i<btnIdA.length;i++){
				RoleMenuModel item=new RoleMenuModel();
				if(model.getId()>0)
					item.setRoleId(model.getId());
				item.setMenuId(Integer.parseInt(btnIdA[i].split("_")[1]));
				ArrayList<String> btn=new ArrayList<String>();
				if(map.containsKey(btnIdA[i].split("_")[1])){
					btn=map.get(btnIdA[i].split("_")[1]);
				}
				btn.add(btnIdA[i].split("_")[0]);
				map.put(btnIdA[i].split("_")[1], btn);
			}
			Iterator<Entry<String, ArrayList<String>>> it=map.entrySet().iterator();
			while(it.hasNext()){
				Entry<String, ArrayList<String>> item=it.next();
				RoleMenuModel role=new RoleMenuModel();
				if(model.getId()>0)
					role.setRoleId(model.getId());
				role.setMenuId(Integer.parseInt(item.getKey()));
				role.setButtons(String.join(",", item.getValue()));
				list.add(role);
			}
		}
		
		return CommonUtil.toJsonWithGson(service.saveRoleAndRoleMenuButtons(model, list));
	}
	
	@RequestMapping(value="/detail",method=RequestMethod.GET)
	public ModelAndView detail(Model model,@RequestParam(value="id",required=false) Integer id){
		ModelAndView view=new ModelAndView();
		view.setViewName("/beetl/role/detail");
		RoleModel role;
		if(id!=null)
			role=service.getByKey(id);
		else
			role=new RoleModel();
		model.addAttribute("model", role);
		return view;
	}
	
	@ResponseBody
	@RequestMapping(value="/delete",method=RequestMethod.POST,produces="application/json;charset=utf-8")
	public String delete(@RequestParam(value="ids[]") List<Object> ids){
		return CommonUtil.toJsonWithGson(service.deleteByKeys(ids));
	}
}
